/*
 * marketsim-eclipsetrader
 *
 * Copyright (C) 2008-2011 Steve Phelps and John Cartlidge
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 */
/*
 *  (C) Victria Ltd. 2007
 */
package victria.marketsim.eclipsetrader;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.InitializingBean;

import victria.marketsim.core.SimRuntimeException;
import victria.marketsim.eclipsetrader.trading.OrderController;

/**
 * Set up listening relationships on visualisation model classes
 * @author john
 *
 */
public class VisualisationController implements InitializingBean {

	/**
	 * The controller for the market state used by the visualisation skins
	 */
	protected MarketStateController marketStateController;	
	
	protected AbstractVisualisationClock marketStateClock;
	
	protected OrderController orderController;
	
	private static VisualisationController singletonInstance;
	
	
	static Logger logger = Logger.getLogger(VisualisationController.class);
	
	public VisualisationController() {
		if (VisualisationController.singletonInstance == null) {
			singletonInstance = this;
		} else {
			logger.error("Attempting to creating new instance of singleton "
					+ this);
			throw new SimRuntimeException(
					"Attempted to create multiple controllers " + this);
		}
	}
	
	public void afterPropertiesSet() throws Exception {
		setListeners();
		startClocks();		
	}
	
	public void startClocks() {
		AbstractVisualisationClock[] clocks = new AbstractVisualisationClock[] {
				marketStateClock
				 };
		for (AbstractVisualisationClock clock : clocks) {
			new Thread(clock).start();
		}
	}	


	/**
	 * Establish listening relationships for models
	 */
	public void setListeners() {
		marketStateController.addListener(orderController);
		orderController.addListener(marketStateController);
		marketStateClock.addListener(marketStateController);	
	}
	
	public void setVisualisationMarketStateController(MarketStateController marketModel) {
		this.marketStateController = marketModel;
	}
	


	public MarketStateController getVisualisationMarketStateController() {
		return marketStateController;
	}


	public OrderController getOrderController() {
		return orderController;
	}


	public void setOrderController(OrderController orderManager) {
		this.orderController = orderManager;
	}

	
	public AbstractVisualisationClock getGuiMarketStateClock() {
		return marketStateClock;
	}

	public void setGuiMarketStateClock(
			AbstractVisualisationClock guiMarketStateClock) {
		this.marketStateClock = guiMarketStateClock;
	}

	public MarketStateController getMarketStateController() {
		return marketStateController;
	}

	public void setMarketStateController(MarketStateController marketStateController) {
		this.marketStateController = marketStateController;
	}

	public AbstractVisualisationClock getMarketStateClock() {
		return marketStateClock;
	}

	public void setMarketStateClock(AbstractVisualisationClock marketStateClock) {
		this.marketStateClock = marketStateClock;
	}


	
}
